using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using MvcExtExample.Models;

namespace MvcExtExample.Handlers
{
    /// <summary>
    /// Summary description for PagerHandler
    /// </summary>
    public class PagerHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            string str = string.Empty;

            //具体的页面数
            int pageIndex;
            int.TryParse(context.Request["pageIndex"], out pageIndex);

            //页面显示条数
            int size = Convert.ToInt32(context.Request["pageSize"]);

            if (pageIndex == 0)
            {
                pageIndex = 1;
            }

            int count;

            List<logPagerModel> list = GetAllLog(size, pageIndex, "OperationType='登录'", out count);

            StringBuilder sb = new StringBuilder();
            foreach (logPagerModel log in list)
            {
                sb.Append("<tr><td>");
                sb.Append(log.CreateTime.ToString());
                sb.Append("</td><td>");
                sb.Append(log.Location);
                sb.Append("</td><td>");
                sb.Append(log.IP);
                sb.Append("</td><td>");
                sb.Append(log.Browser);
                sb.Append("</td></tr>");
            }
            str = sb.ToString();
            context.Response.Write(str);   
        }

        public List<logPagerModel> GetAllLog(int size, int index, string where, out int OutTotalCount)
        {
            logPagerModel log = null;
            List<logPagerModel> list = new List<logPagerModel>();
            DataTable dt = null;
            framework.SqlHelperCS.SqlHelper cs = new framework.SqlHelperCS.SqlHelper();
            //分页参数
            string[] paramValue = 
            { 
                "Tb_LogOperation",//表名
                "*",//返回字段
                "ID",//主键标识列
                where,//where条件
                "ID asc",//排序必须跟有 asc 或 desc
                "1",//排序规则 1:正序asc 2:倒序desc 3:多列排序方法
                "0",//记录总数 0:会返回总记录
                ""+size,//页面大小
                ""+index//当前页
            };
            dt = cs.SqlGetDataTable("pro_logPage", CommandType.StoredProcedure, paramValue, out OutTotalCount);//这里的OutTotalCount算的是错误的！！！！
            foreach (DataRow dr in dt.Rows)
            {
                log = new logPagerModel();
                log.CreateTime = dr["CreateTime"].ToString();
                if (dr["IP"]!=null)
                {
                    log.IP = dr["IP"].ToString();
                    if (dr["IP"].ToString()!="")
                        log.Location = IPLocation.IPLocation.IPLocate(HttpContext.Current.Server.MapPath("../") + "Content\\qqwry.dat", dr["IP"].ToString());
                }
                if (dr["IP"] != null)
                {
                    log.IP = dr["IP"].ToString();
                }
                if (dr["Browser"] != null)
                {
                    log.Browser = dr["Browser"].ToString();
                }
                list.Add(log);
            }
            return list;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}